﻿using System;
using System.Collections.Generic;
using System.Text;
using Common.Common.Html;
using System.Xml.Linq;
using System.Xml;
using System.Reflection;
using System.Linq;
namespace System
{
    public static class Tools
    {
        private static List<HtmlModel> words = null;
        private static int Language = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["Language"]);
        /// <summary>
        /// 得到关键字的显示文字
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetFaceText(this string key)
        {
            if (words == null)
            {
                InitWords();
            }
            var query = from word in words where word.Key.Equals(key) select word;
            if (query.Count() > 0)
            {

                HtmlModel model = query.First();
                return GetString(model);
            }
            else
            {
                return GetFaceText("NoDefinition");
            }
        }
        /// <summary>
        /// 根据显示文字信息筛选文字
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private static string GetString(HtmlModel model)
        {
            string _value = "";
            switch (Language)
            {
                case 0: _value = model.Chinese;
                    break;
                case 1: _value = model.English;
                    break;
                case 2: _value = model.Arabic;
                    break;
                case 3: _value = model.French;
                    break;
                case 4: _value = model.German;
                    break;
                case 5: _value = model.Italian;
                    break;
                case 6: _value = model.Japanese;
                    break;
                case 7: _value = model.Portuguese;
                    break;
                case 8: _value = model.Russian;
                    break;
                case 9: _value = model.Spain;
                    break;
                default: _value = model.Chinese;
                    break;
            }
            return _value;
        }

        /// <summary>
        /// 从xml中查询定义好的信息
        /// </summary>
        private static void InitWords()
        {
            words = new List<HtmlModel>();
            XmlNode _node = GetXMLInformation();
            XmlNodeList _xn = _node.SelectNodes("key");
            foreach (XmlNode node in _xn)
            {
                HtmlModel info = new HtmlModel();
                info.Key = node.Attributes["Name"].Value;
                XmlNodeList xnl = node.ChildNodes;
                foreach (XmlNode el in xnl)
                {
                    if (el.Name.Equals("Chinese"))
                    {
                        info.Chinese = el.InnerText.Trim();
                    }
                    else if (el.Name.Equals("Arabic"))
                    {
                        info.Arabic = el.InnerText.Trim();
                    }
                    else if (el.Name.Equals("English"))
                    {
                        info.English = el.InnerText.Trim();
                    }
                    else if (el.Name.Equals("French"))
                    {
                        info.French = el.InnerText.Trim();
                    }
                    else if (el.Name.Equals("German"))
                    {
                        info.German = el.InnerText.Trim();
                    }
                    else if (el.Name.Equals("Italian"))
                    {
                        info.Italian = el.InnerText.Trim();
                    }
                    else if (el.Name.Equals("Japanese"))
                    {
                        info.Japanese = el.InnerText.Trim();
                    }
                    else if (el.Name.Equals("Portuguese"))
                    {
                        info.Portuguese = el.InnerText.Trim();
                    }
                    else if (el.Name.Equals("Russian"))
                    {
                        info.Russian = el.InnerText.Trim();
                    }
                    else if (el.Name.Equals("Spain"))
                    {
                        info.Spain = el.InnerText.Trim();
                    }
                }
                words.Add(info);
            }
        }

        /// <summary>
        /// 读取xml文件
        /// </summary>
        /// <returns></returns>
        private static XmlNode GetXMLInformation()
        {
            Assembly assem = Assembly.GetExecutingAssembly();

            //xml文件名，区分大小写
            StringBuilder xmlFileName = new StringBuilder();
            xmlFileName.Append(assem.GetName().Name);
            xmlFileName.Append(".");
            xmlFileName.Append("Common.Html.Language.xml");
            System.IO.Stream xmlFileStream = assem.GetManifestResourceStream(xmlFileName.ToString());

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFileStream);
            XmlNode x = xmlDoc.SelectSingleNode("keys");
            return x;
        }
    }
}
